package hyl.core.db;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import hyl.core.Amy;
import hyl.core.MyFun;

/* 替换模式 */
public class MySqlSelect2 {
	// 脚本
	StringBuilder _sql = new StringBuilder();
	// 统计脚本
	Map<Integer, ISelect> _items = new HashMap<Integer, ISelect>();
	// 分页定位
	String _page;
	MyDB db;

	public static MySqlSelect2 getInstance(String sql) {
		return new MySqlSelect2(sql);
	}

	private MySqlSelect2(String sql) {
		db = Amy.getDB1();
		iniSql(sql);
	}

	/* 添加sql */
	public void iniSql(String sql) {
		if (!MyFun.isEmpty(sql)) {
			_sql.setLength(0);
			_sql.append(sql);
		}
	}

	public void addParams(Integer endpoint, SelectWhere sw) {
		// 不可以加空判断，因为 {1} 必须被替换
		_items.put(endpoint, sw);
	}

	/* 添加排序 */
	public void orderby(Integer endpoint, String order) {
		_items.put(endpoint, new SelectString(order));
	}

	/* 添加分页区间 如果 size 为空 size默认等于100 */
	public void fromto(Integer endpoint, Integer index, Integer size) {
		if (index == null) {
			if (size == null || size == 0)
				_page = " limit 100";
			else
				_page = " limit " + size;
		} else {
			if (size == null || size == 0)
				_page = " limit " + index + ",100";
			else
				_page = " limit " + index + "," + size;
		}
		_items.put(endpoint, new SelectString(_page));
	}

	/* { rs:[数据]， total：共多少条 } */
	/* 返回对象 rs：结果集合 其他的以 countsql 字段名为键值 */
	private String prepare(List<Object> _params) {
		String sqlall = _sql.toString();
		if (sqlall.length() == 0) {
			return null;
		}
		// https://www.cnblogs.com/iyangyuan/p/4809582.html 正则表达式误解
		for (Entry<Integer, ISelect> item : _items.entrySet()) {
			String key = "{" + item.getKey() + "}";
			ISelect val = item.getValue();
			if (val instanceof SelectString) {
				sqlall = sqlall.replace(key, ((SelectString) val)._str);
			} else if (val instanceof SelectWhere) {
				SelectWhere sw = (SelectWhere) val;
				if (sw.isEmpty())
					sqlall = sqlall.replace(key, "");
				else {
					sqlall = sqlall.replace(key, sw._where.toString());
					_params.addAll(sw._params);
				}
			}
		}
		return sqlall;
	}

	public List<Map<String, Object>> getList() {
		List<Object> _params = new ArrayList<Object>();
		String sqlall = prepare(_params);
		if (sqlall == null)
			return null;
		Object[] ps1 = _params.toArray();
		List<Map<String, Object>> result = db.queryList(sqlall, ps1);
		return result;
	}

	public List<List<Object>> getList2D() {
		List<Object> _params = new ArrayList<Object>();
		String sqlall = prepare(_params);
		if (sqlall == null)
			return null;
		Object[] ps1 = _params.toArray();
		// System.out.println(sqlall);
		// System.out.println(ps1);
		List<List<Object>> result = db.queryList2D(sqlall, ps1);
		return result;
	}

	public Map<String, Object> getMap() {
		String sqlall = _sql.toString();
		Object[] ps1;
		List<Object> _params = new ArrayList<Object>();
		if (sqlall.length() == 0) {
			return null;
		}
		for (Entry<Integer, ISelect> item : _items.entrySet()) {
			String key = "{" + item.getKey() + "}";
			ISelect val = item.getValue();
			if (val instanceof SelectWhere) {
				SelectWhere sw = (SelectWhere) val;
				if (sw.isEmpty()) {
					sqlall = sqlall.replace(key, "");
				} else {
					sqlall = sqlall.replace(key, sw._where.toString());
					_params.addAll(sw._params);
				}
			}
		}
		ps1 = _params.toArray();
		Map<String, Object> result = db.queryMap(sqlall, ps1);
		return result;
	}
}
